// #include <Arduino.h>
// #include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <string>
// #include <ESPAsyncTCP.h>

/*
const char * ssid = "CMCC-w4cv";
const char * password = "b3t4t93d";
*/
const char * ssid = "Xiaomi-summer";
const char * password = "15298270785xm";
const int Relay=4;
int m_port = 8888;  //Port number
WiFiServer server(m_port);
int count = 0 ;
char char_test = '1';
String recive_string;
std::string m_str;

void setup() {
  Serial.begin(115200);
  pinMode(Relay, OUTPUT);
  delay(1000);
  Serial.println("Serial start!");

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
      Serial.println("WiFi Failed");
      while(1) {
          delay(1000);
      }
    }
  Serial.println("WiFi Connected!");
  
  //print self's IP information
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  
  server.begin();
  Serial.print("Open Telnet and connect to IP:");
  Serial.print(WiFi.localIP());
  Serial.print(" on port ");
  Serial.println(m_port);

}

void loop() {

  WiFiClient client = server.available();
  
  if (client) {
    if(client.connected())
    {
      Serial.println("Client Connected");
    }
    
    while(client.connected()){      
      while(client.available()>0){
        // read data from the connected client
        char recv = client.read();
        if(recv-48 == 1)
          digitalWrite(Relay,HIGH);
        else
          digitalWrite(Relay,LOW);
        m_str.push_back(recv); 
        Serial.write(recv); 
        // Serial.write(m_str); 
        count += 1;
      }
    }
  }
  // digitalWrite(Relay,HIGH);
  // delay(1000);
  // Serial.println("This is Serial printing...");
  // digitalWrite(Relay,LOW);
  // delay(1000);
  // put your main code here, to run repeatedly:
}